package com.abel.web.controller;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.abel.domain.HelpfulInformation;
import com.abel.domain.PersonProfile;
import com.abel.domain.types.HelpfulInfoStatus;
import com.abel.manager.PersonProfileManager;
import com.abel.util.UserSessionLocator;
import com.abel.web.view.HelpfulInfoViewBean;

public class HelpfulInfoController extends SimpleFormController{

	protected final Log logger = LogFactory.getLog(getClass());
	
	private PersonProfileManager personProfileManager;
	
	private String previousView = null;

	
	/**
	 * @return the previousView
	 */
	public String getPreviousView() {
		return previousView;
	}



	/**
	 * @param previousView the previousView to set
	 */
	public void setPreviousView(String previousView) {
		this.previousView = previousView;
	}



	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {

		ModelAndView mav = null;
		
		Map<String, Object> modelMap = new HashMap<String, Object>();
		
		String action = request.getParameter("previous") != null ? "previous" : "continue";
		
		Enumeration en = request.getParameterNames();
		while (en.hasMoreElements()) {
			String key = en.nextElement().toString();
			logger.error("param::" + key + "=" + request.getParameter(key));
		}
		
		HelpfulInfoViewBean bean = (HelpfulInfoViewBean )command;
		//setting domain
		HelpfulInformation helpfulInformation = new HelpfulInformation();
		helpfulInformation.setAccessToCar(HelpfulInfoStatus.getStatusCode(bean.getAccessToCar()));
		helpfulInformation.setEmployed(HelpfulInfoStatus.getStatusCode(bean.getEmployed()));
		helpfulInformation.setTravelPercentage(bean.getHowMuchTravel() != null && !bean.getHowMuchTravel().equals("") ? Integer.parseInt(bean.getHowMuchTravel()) : 0);
		helpfulInformation.setWillingToRelocate(HelpfulInfoStatus.getStatusCode(bean.getWillingToRelocate()));
		helpfulInformation.setWillingToTravel(HelpfulInfoStatus.getStatusCode(bean.getWillingtoTravel()));
		
		try {
			personProfileManager.updateHelpfulInfo(helpfulInformation);
			
		} catch (Exception e) {

			logger.error("@HelpfulInfoController : onSubmit : exception " + e);
			errors.rejectValue("", "error.null",null,"A system error occurred while entering your information. Please log out and log back in.");
			return showForm(request,response,errors);

		}
		if (action != null && action.equals("previous")) {
			mav = new ModelAndView("redirect:" + getPreviousView());
		}
		else {
			mav = new ModelAndView("redirect:" + getSuccessView());
		}
		mav.addAllObjects(modelMap);
		return mav;

	}
	


	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		
		//get helpfulInformation from threadlocal
		PersonProfile personProfile = UserSessionLocator.getUserSession().getPerson().getApplication().getPersonProfile();
		HelpfulInfoViewBean viewBean = new HelpfulInfoViewBean();
		
		if (personProfile.getHelpfulInformation() != null) {
			
			//information already exists - returning user
			HelpfulInformation helpfulInformation = personProfile.getHelpfulInformation();
			
			viewBean.setAccessToCar(HelpfulInfoStatus.getStatus(helpfulInformation.getAccessToCar()));
			viewBean.setEmployed(HelpfulInfoStatus.getStatus(helpfulInformation.getEmployed()));
			viewBean.setHowMuchTravel(Integer.toString(helpfulInformation.getTravelPercentage()));
			viewBean.setWillingToRelocate(HelpfulInfoStatus.getStatus(helpfulInformation.getWillingToRelocate()));
			viewBean.setWillingtoTravel(HelpfulInfoStatus.getStatus(helpfulInformation.getWillingToTravel()));
		}

		return viewBean;
	}

	/**
	 * @param personProfileManager the personProfileManager to set
	 */
	public void setPersonProfileManager(PersonProfileManager personProfileManager) {
		this.personProfileManager = personProfileManager;
	}



}
